<?php


namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\InfoModel;
use app\api\model\Invoice as InvoiceModel;
use PHPExcel_Cell;
use think\Request;

class ExcelController extends BaseController
{
    public function excelCopy(Request $request)
    {
        $params = $request->post();
        $month = $params['month'];
        $state = $params['state'];
        $prefix = $params['prefix'];

        $data = InfoModel::getInfos($month);
        set_time_limit(0);
        ini_set("memory_limit", "1024M"); // 设置php可使用内存
        $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
        if (!\PHPExcel_Settings::setCacheStorageMethod($cacheMethod)) {
            die($cacheMethod . " 缓存方法不可用" . EOL);
        }
        $date = strtotime($month . '01');
        $formattedDate = date('Y年n月', $date);
        if ($data['plan']['state'] == 1) {
            $t = '现金预算';
        } else if ($data['plan']['state'] == 2) {
            $t = '现金计划一';
        } else if ($data['plan']['state'] == 3) {
            $t = '现金计划二';
        } else if ($data['plan']['state'] == 4) {
            $t = '现金计划三';
        }
        $title = $formattedDate . ' ' . $t;
        $PHPExcel = new \PHPExcel();
        $PHPSheet = $PHPExcel->getActiveSheet();
        // 设置个表格标题
        $PHPSheet->setTitle($title);

        $PHPSheet->setCellValue("A2", "序号");
        $PHPSheet->setCellValue("B2", "执行日期");
        $PHPSheet->setCellValue("C2", "日");
        $PHPSheet->setCellValue("D2", "内容");
        $PHPSheet->setCellValue("E2", "港币");
        $PHPSheet->setCellValue("H2", "人民币\r\n(填写实数)");
        $PHPSheet->getStyle('H2:H2')->getAlignment()->setWrapText(TRUE);
        $PHPSheet->setCellValue("I2", "垫付");
        $PHPSheet->setCellValue("J2", "流水(填写实数)\r\n收入=支出");
        $PHPSheet->getStyle('J2:J2')->getAlignment()->setWrapText(TRUE);

        foreach ($data['categorys'][0] as $index => $value) {
            $column = PHPExcel_Cell::stringFromColumnIndex(11 + $index * 2);
            $column_inncome = PHPExcel_Cell::stringFromColumnIndex(11 + $index * 2);
            $column_outcome = PHPExcel_Cell::stringFromColumnIndex(11 + $index * 2 + 1);
            $PHPSheet->setCellValue($column_inncome . "3", "收入");
            $PHPSheet->getColumnDimension($column_inncome)->setWidth(19);
            $PHPSheet->setCellValue($column_outcome . "3", "支出");
            $PHPSheet->getColumnDimension($column_outcome)->setWidth(19);
            $PHPSheet->setCellValue($column . "2", $value['title']);
            $PHPSheet->mergeCells($column_inncome . '2:' . $column_outcome . '2');
        }
        $start = count($data['categorys'][0]) * 2;
        $end_column = PHPExcel_Cell::stringFromColumnIndex(11 + $start + 2);
        //动态标头
        $column_balance = PHPExcel_Cell::stringFromColumnIndex(11 + $start);
        $column_money = PHPExcel_Cell::stringFromColumnIndex(11 + $start + 1);
        $column_remark = PHPExcel_Cell::stringFromColumnIndex(11 + $start + 2);
        $PHPSheet->setCellValue($column_balance . "2", "余额");
        $PHPSheet->setCellValue($column_money . "2", "实际金额");
        $PHPSheet->setCellValue($column_remark . "2", "备注");

        $PHPSheet->setCellValue("E3", "金额");
        $PHPSheet->setCellValue("F3", "预算\r\n汇率");
        $PHPSheet->getStyle('F3:F3')->getAlignment()->setWrapText(TRUE);
        $PHPSheet->setCellValue("G3", "真实\r\n汇率");
        $PHPSheet->getStyle('G3:G3')->getAlignment()->setWrapText(TRUE);
        $PHPSheet->setCellValue("J3", "收入");
        $PHPSheet->setCellValue("K3", "支出");
        $PHPSheet->mergeCells($column_balance . '2:' . $column_balance . '3');
        $PHPSheet->mergeCells($column_money . '2:' . $column_money . '3');
        $PHPSheet->mergeCells($column_remark . '2:' . $column_remark . '3');

        //设置表格宽度
        $PHPSheet->getColumnDimension('A')->setWidth(10);
        $PHPSheet->getColumnDimension('B')->setWidth(22);
        $PHPSheet->getColumnDimension('C')->setWidth(9);
        $PHPSheet->getColumnDimension('D')->setWidth(68);
        $PHPSheet->getColumnDimension('E')->setWidth(19);
        $PHPSheet->getColumnDimension('F')->setWidth(11);
        $PHPSheet->getColumnDimension('G')->setWidth(11);
        $PHPSheet->getColumnDimension('H')->setWidth(19);
        $PHPSheet->getColumnDimension('I')->setWidth(19);
        $PHPSheet->getColumnDimension('J')->setWidth(19);
        $PHPSheet->getColumnDimension('K')->setWidth(19);
        $PHPSheet->getColumnDimension($column_balance)->setWidth(20);
        $PHPSheet->getColumnDimension($column_money)->setWidth(20);
        $PHPSheet->getColumnDimension($column_remark)->setWidth(30);

        $PHPSheet->mergeCells('A1:'.$end_column.'1')
            ->mergeCells('E2:G2')
            ->mergeCells('J2:K2')
            ->mergeCells('A2:A3')
            ->mergeCells('B2:B3')
            ->mergeCells('C2:C3')
            ->mergeCells('D2:D3')
            ->mergeCells('H2:H3')
            ->mergeCells('I2:I3');

        $PHPSheet->setCellValue("A1", ($title));
        $PHPSheet->getStyle('A1:' . $end_column . '1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $PHPSheet->getStyle('A1:' . $end_column . '1')->getFont()->setBold(true)->getColor()->setARGB('333333');
        $PHPSheet->getStyle('A1:' . $end_column . '1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID);
        $PHPSheet->getStyle('A1:' . $end_column . '1')->applyFromArray(array(
            'borders' => array(
                'allborders' => array(
                    'style' => \PHPExcel_Style_Border::BORDER_THIN,//细边框
                    'color' => array('argb' => '000000'),
                )
            )
        ));
        //设置垂直居中
        $PHPSheet->getStyle('A2:' . $end_column . '2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $PHPSheet->getStyle('A2:' . $end_column . '2')->getFont()->setBold(true)->getColor()->setARGB('333333');
        $PHPSheet->getStyle('A2:' . $end_column . '2')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('F8F8F9');
        $PHPSheet->getStyle('A2:' . $end_column . '2')->applyFromArray(array(
            'borders' => array(
                'allborders' => array(
                    'style' => \PHPExcel_Style_Border::BORDER_THIN,//细边框
                    'color' => array('argb' => '000000'),
                )
            )
        ));

        //设置垂直居中
        $PHPSheet->getStyle('A3:' . $end_column . '3')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $PHPSheet->getStyle('A3:' . $end_column . '3')->getFont()->setBold(true)->getColor()->setARGB('333333');
        $PHPSheet->getStyle('A3:' . $end_column . '3')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('F8F8F9');
        $PHPSheet->getStyle('A3:' . $end_column . '3')->applyFromArray(array(
            'borders' => array(
                'allborders' => array(
                    'style' => \PHPExcel_Style_Border::BORDER_THIN,//细边框
                    'color' => array('argb' => '000000'),
                )
            )
        ));

        $PHPSheet->getStyle('A:' . $end_column)->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

        $PHPSheet->getDefaultRowDimension()->setRowHeight(35);
        $list = [$data['total']['header']];
        foreach ($data['data'] as $array) {
            foreach ($array as $temp) {
                array_push($list, $temp);
            }
        }
        array_push($list, $data['total']['footer']);
        foreach ($list as $index => $temp) {
            $i = $index + 4;
            $PHPSheet->getStyle('A' . $i . ':' . $end_column . $i)->applyFromArray(array(
                'borders' => array(
                    'allborders' => array(
                        'style' => \PHPExcel_Style_Border::BORDER_THIN,
                        'color' => array('argb' => '000000'),
                    )
                )
            ));
            if ($i == 4) {
                $PHPSheet->getStyle('A' . $i . ':' . $end_column . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('E3EBCD');
            } else if ($temp['type'] != 3) {
                $PHPSheet->getStyle('A' . $i . ':' . $end_column . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('D8E4BC');
            } else if ($temp['source'] == 'system') {
                $PHPSheet->getStyle('A' . $i . ':' . 'A' . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('FFA500');
            }
            if($temp['category'] == 'A'){
                $PHPSheet->getStyle('A' . $i . ':' . $end_column . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('F8D0D5');
            }
            if(array_key_exists('is_light', $temp) && $temp['is_light'] == 1){
                $PHPSheet->getStyle('A' . $i . ':' . $end_column . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('FFFFB9');
            }

            if(array_key_exists('index', $temp) && $temp['index']){
                $PHPSheet->setCellValue("A" . $i, '' . $temp['index']);
            }
            if(array_key_exists('day', $temp) && $temp['day']){
                $PHPSheet->setCellValue("B" . $i, '' . $temp['issue'].' '.$temp['weekday']);
                $PHPSheet->setCellValue("C" . $i, '' . $temp['day']);
            }
            if(array_key_exists('title', $temp) && $temp['title']){
                $PHPSheet->setCellValue("D" . $i, '' . $temp['title']);
                if($temp['type'] == 3){
                    $PHPSheet->getStyle('D' . $i)->getAlignment()
                        ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT)
                        ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                }
            }
            if(array_key_exists('detail_money', $temp) && $temp['detail_money']){
                $PHPSheet->getStyle('E' . $i)->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
                    ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);

                if($temp['id'] == null){
                    $PHPSheet->getStyle('E' . $i)->getNumberFormat(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER)->setFormatCode('#,##0.00');
                    $PHPSheet->setCellValue("E" . $i, '' . $temp['detail_money']);
                }else{
                    $PHPSheet->setCellValue("E" . $i, '$' . number_format($temp['detail_money'], 2, '.', ','));
                }
            }

            if(array_key_exists('budget_rate', $temp) && $temp['budget_rate']){
                $PHPSheet->setCellValue("F" . $i, '' . $temp['budget_rate']);
            }else if($i == 4){
                $PHPSheet->getStyle('F' . $i . ':' . 'F' . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('DA9694');
            }

            if(array_key_exists('real_rate', $temp) && $temp['real_rate']){
                $PHPSheet->setCellValue("G" . $i, '' . $temp['real_rate']);
            }
            if($i == 4 && $state == 4 && in_array('real_rate', $data['total']['warning_fields'])){
                $PHPSheet->getStyle('G' . $i . ':' . 'G' . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('FFFE9B');
            }

            if(array_key_exists('detail_rmb', $temp) && $temp['detail_rmb']){
                $PHPSheet->getStyle('H' . $i)->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
                    ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $PHPSheet->getStyle('H' . $i)->getNumberFormat(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER)->setFormatCode('#,##0.00');
                $PHPSheet->setCellValue("H" . $i, '' . $temp['detail_rmb']);
            }

            if(array_key_exists('detail_payment', $temp) && $temp['detail_payment']){
                $PHPSheet->getStyle('I' . $i)->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
                    ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $PHPSheet->getStyle('I' . $i)->getNumberFormat(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER)->setFormatCode('#,##0.00');
                $PHPSheet->setCellValue("I" . $i, '' . $temp['detail_payment']);
            }

            if(array_key_exists('income', $temp) && $temp['income']){
                $PHPSheet->getStyle('J' . $i)->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
                    ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $PHPSheet->getStyle('J' . $i)->getNumberFormat(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER)->setFormatCode('#,##0.00');
                $PHPSheet->setCellValue("J" . $i, '' . $temp['income']);
            }
            if($i == 4 && array_key_exists('income', $temp) &&  ($temp['income'] != $temp['outcome'])){
                $PHPSheet->getStyle('J' . $i . ':' . 'J' . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('DA9694');
            }
            if($i == 4 && $state == 4 && in_array('income', $data['total']['warning_fields'])){
                $PHPSheet->getStyle('J' . $i . ':' . 'J' . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('FFFE9B');
            }

            if(array_key_exists('outcome', $temp) && $temp['outcome']){
                $PHPSheet->getStyle('K' . $i)->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
                    ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $PHPSheet->getStyle('K' . $i)->getNumberFormat(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER)->setFormatCode('#,##0.00');
                $PHPSheet->setCellValue("K" . $i, '' . $temp['outcome']);
            }
            if($i == 4 && array_key_exists('outcome', $temp) &&  ($temp['income'] != $temp['outcome'])){
                $PHPSheet->getStyle('K' . $i . ':' . 'K' . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('DA9694');
            }
            if($i == 4 && $state == 4 && in_array('outcome', $data['total']['warning_fields'])){
                $PHPSheet->getStyle('K' . $i . ':' . 'K' . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('FFFE9B');
            }

            foreach ($data['categorys'][0] as $indexI => $valueI) {
                $column_inncome = PHPExcel_Cell::stringFromColumnIndex(11 + $indexI * 2);
                $column_outcome = PHPExcel_Cell::stringFromColumnIndex(11 + $indexI * 2 + 1);

                $income_field = 'detail_income_'.$valueI['id'];
                if(array_key_exists($income_field, $temp) && $temp[$income_field]){
                    $PHPSheet->getStyle($column_inncome . $i)->getAlignment()
                        ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
                        ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                    $PHPSheet->getStyle($column_inncome . $i)->getNumberFormat(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER)->setFormatCode('#,##0.00');
                    $PHPSheet->setCellValue($column_inncome . $i, '' . $temp[$income_field]);
                }
                if($i == 4 && $state == 4 && in_array($income_field, $data['total']['warning_fields'])){
                    $PHPSheet->getStyle($column_inncome . $i . ':' . $column_inncome . $i)
                        ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                        ->getStartColor()->setARGB('FFFE9B');
                }

                $outcome_field = 'detail_outcome_'.$valueI['id'];
                if(array_key_exists($outcome_field, $temp) && $temp[$outcome_field]){
                    $PHPSheet->getStyle($column_outcome . $i)->getAlignment()
                        ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
                        ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                    $PHPSheet->getStyle($column_outcome . $i)->getNumberFormat(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER)->setFormatCode('#,##0.00');
                    $PHPSheet->setCellValue($column_outcome . $i, '' . $temp[$outcome_field]);
                }
                if($i == 4 && $state == 4 && in_array($outcome_field, $data['total']['warning_fields'])){
                    $PHPSheet->getStyle($column_outcome . $i . ':' . $column_outcome . $i)
                        ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                        ->getStartColor()->setARGB('FFFE9B');
                }
            }

            if(array_key_exists('balance', $temp) && $temp['balance']){
                $PHPSheet->getStyle($column_balance . $i)->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
                    ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $PHPSheet->getStyle($column_balance . $i)->getNumberFormat(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER)->setFormatCode('#,##0.00');
                $PHPSheet->setCellValue($column_balance . $i, '' . $temp['balance']);
            }
            if($i == 4 && array_key_exists('balance', $temp) && ($temp['balance'] > 100 || $temp['balance'] < -100)){
                $PHPSheet->getStyle($column_balance . $i . ':' . $column_balance . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('DA9694');
            }
            if($i == 4 && $state == 4 && in_array('balance', $data['total']['warning_fields'])){
                $PHPSheet->getStyle($column_balance . $i . ':' . $column_balance . $i)
                    ->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)
                    ->getStartColor()->setARGB('FFFE9B');
            }

            if(array_key_exists('money', $temp) && $temp['money']){
                $PHPSheet->getStyle($column_money . $i)->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
                    ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $PHPSheet->getStyle($column_money . $i)->getNumberFormat(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER)->setFormatCode('#,##0.00');
                $PHPSheet->setCellValue($column_money . $i, '' . $temp['money']);
            }

            if(array_key_exists('remark', $temp) && $temp['remark']){
                $PHPSheet->getStyle($column_remark . $i)->getAlignment()
                    ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT)
                    ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $PHPSheet->setCellValue($column_remark . $i, '' . $temp['remark']);
            }

        }
        $last = 4 + count($list);
        $PHPSheet->getStyle('A' . $last)->getAlignment()
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT)
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $PHPSheet->getStyle('A'.$last.':A'.$last)->getAlignment()->setWrapText(TRUE);
        $PHPSheet->getRowDimension($last)->setRowHeight(70);
        $PHPSheet->mergeCells('A'.$last.':'.$end_column.$last);
        $PHPSheet->setCellValue("A".$last, $data['plan']['remark']);

        $title = $formattedDate . $t;
        $PHPExcel->setActiveSheetIndex(0);
        $objWriter = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel2007");

        if (!is_dir('./files/copy/'.$month)) {
            mkdir('./files/copy/'.$month, 0777, true);
        }
        $relative_name = Constant::getFileDir() . '/copy/'.$month.'/' . $title . '.xlsx';
        $file = './files/copy/'.$month.'/' . $title . '.xlsx';
        $objWriter->save($file);
        self::down(config('const.copy_url_api').'?name='.urlencode($title . '.xlsx').'&url='.urlencode($prefix.$title . '.xlsx').'&month='.$month);
        return [
            'name' => $title . '.xlsx',
            'path' => $relative_name,
        ];
    }

    public function down($url)
    {
        $ssl = substr($url, 0, 8) == "https://" ? TRUE : FALSE;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        if ($ssl) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    function base64imgsave($img){
        //文件夹日期
//        $issue = date("Ymd");
//        $root_path = Constant::getFileDir();
//        $file_folder = $root_path . '/' . $issue;
//        if(!is_dir($file_folder)){
//            mkdir($file_folder,0777,true);
//        }
        $types = empty($types)? array('jpg', 'gif', 'png', 'jpeg'):$types;
        $img = str_replace(array('_','-'), array('/','+'), $img);
        $b64img = substr($img, 0,100);
        if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $b64img, $matches)){
            $type = $matches[2];
            if(!in_array($type, $types)){
                return array('status'=>1,'info'=>'图片格式不正确，只支持 jpg、gif、png、jpeg哦！','url'=>'');
            }
            $img = str_replace($matches[1], '', $img);
            $img = base64_decode($img);
            //推送图片
            $this->http_post(config('const.push_img_url'), ['img'=>$img]);
            $ary['status'] = 1;
            $ary['info'] = '保存图片成功';
            return $ary;
        }
        $ary['status'] = 0;
        $ary['info'] = '请选择要上传的图片';
        return $ary;
    }
    function http_post($url, $post_data, $header = [], $proxy = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
}